# encoding=utf-8
import traceback
from abc import ABC
import os

import flask
from seg_system import ApplicationConfig
from seg_common.FileService import DirMakeFactory
from seg_common.Paser import ParserTools
from seg_common.Annotation import CommonAnnotation


class VascularDirFactory(DirMakeFactory, ABC):
    def __init__(self):
        super(VascularDirFactory, self).__init__()
        self.NORMAL_0 = ""
        self.NORMAL_1 = ""
        self.NORMAL_1_0 = ""
        self.NORMAL_1_1 = ""
        self.NORMAL_2 = ""
        self.NORMAL_3 = ""
        self.NORMAL_TMP_0 = ""  # Color的信息和原来的图片合并
        self.NORMAL_TMP_1_0 = ""  # Branch_point图片合并
        self.NORMAL_TMP_4 = ""  # 前台可交互绘图的参数
        self.NORMAL_TMP_5 = ""  # cnbd运算得到的神经交叉点坐标

        self.ENHANCE_0 = ""
        self.ENHANCE_1 = ""
        self.ENHANCE_1_0 = ""
        self.ENHANCE_1_1 = ""
        self.ENHANCE_2 = ""
        self.ENHANCE_3 = ""
        self.ENHANCE_TMP_0 = ""  # Color的信息和原来的图片合并
        self.ENHANCE_TMP_1_0 = ""  # Branch_point
        self.ENHANCE_TMP_4 = ""  # 前台可交互绘图的参数
        self.ENHANCE_TMP_5 = ""  # cnbd运算得到的神经交叉点坐标

    @CommonAnnotation.override()
    def path_init_with_request(self, request: flask.Request, save_dir: str = None):
        user_name, token = ParserTools.parser_name_token_from_request(request)
        return self.temp_dir_create(save_dir=save_dir, user_name=user_name, token=token)

    @CommonAnnotation.override()
    def path_init(self, **kwargs):
        save_dir, user_name, token = super().base_parser(**kwargs)

        if user_name and token:
            USER_FOLDER = os.path.join(ApplicationConfig.PathConfig.BASE_SAVE_PATH,
                                       user_name, token)
        else:
            USER_FOLDER = os.path.join(ApplicationConfig.PathConfig.BASE_SAVE_PATH,
                                       ApplicationConfig.PathConfig.DEFAULT_USER_NAME,
                                       ApplicationConfig.PathConfig.DEFAULT_USER_TOKEN)

        if save_dir:
            USER_FOLDER = os.path.join(save_dir, USER_FOLDER)

        VASCULAR_FOLDER = os.path.join(USER_FOLDER, ApplicationConfig.PathConfig.VASCULAR_BASE_FOLDER)
        NORMAL_FOLDER = os.path.join(VASCULAR_FOLDER, ApplicationConfig.PathConfig.NORMAL_SAVE_PATH)
        ENHANCE_FOLDER = os.path.join(VASCULAR_FOLDER, ApplicationConfig.PathConfig.ENHANCE_SAVE_PATH)

        self.NORMAL_0 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ZERO_FOLDER)
        self.NORMAL_1 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_FOLDER)
        self.NORMAL_1_0 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_SUB_FOLDER)
        self.NORMAL_1_1 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_THIRD_FOLDER)
        self.NORMAL_2 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TWO_FOLDER)
        self.NORMAL_3 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_THREE_FOLDER)
        self.NORMAL_TMP_0 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_ZERO_FOLDER)
        self.NORMAL_TMP_1_0 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_ONE_SUB_FOLDER)
        self.NORMAL_TMP_4 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_FOUR_FOLDER)
        self.NORMAL_TMP_5 = os.path.join(NORMAL_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_FIVE_FOLDER)

        self.ENHANCE_0 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ZERO_FOLDER)
        self.ENHANCE_1 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_FOLDER)
        self.ENHANCE_1_0 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_SUB_FOLDER)
        self.ENHANCE_1_1 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_ONE_THIRD_FOLDER)
        self.ENHANCE_2 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TWO_FOLDER)
        self.ENHANCE_3 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_THREE_FOLDER)
        self.ENHANCE_TMP_0 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_ZERO_FOLDER)
        self.ENHANCE_TMP_1_0 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_ONE_SUB_FOLDER)
        self.ENHANCE_TMP_4 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_FOUR_FOLDER)
        self.ENHANCE_TMP_5 = os.path.join(ENHANCE_FOLDER, ApplicationConfig.PathConfig.VASCULAR_TMP_FIVE_FOLDER)

        return self.NORMAL_0

    @CommonAnnotation.override()
    def temp_dir_create(self, **kwargs):
        USER_FOLDER = self.path_init(**kwargs)
        if os.path.exists(USER_FOLDER):
            return True, [{'name': USER_FOLDER, 'success': 'ok'}]

        try:
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_0)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_1)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_1_0)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_1_1)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_2)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_3)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_TMP_0)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_TMP_1_0)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_TMP_4)
            ApplicationConfig.PathConfig.make_dir(self.NORMAL_TMP_5)

            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_0)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_1)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_1_0)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_1_1)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_2)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_3)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_TMP_0)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_TMP_1_0)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_TMP_4)
            ApplicationConfig.PathConfig.make_dir(self.ENHANCE_TMP_5)
        except Exception as e:
            print(traceback.format_exc())
            return False, [{'name': USER_FOLDER, 'error': str(e)}]
        return True, [{'name': USER_FOLDER, 'success': 'ok'}]
